package com.app.controller 
{
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	import org.puremvc.as3.interfaces.INotification;
	
	import com.peachcastle.events.LoaderEvent;
	import com.peachcastle.net.QueueLoader;
	
	import com.app.AppFacade;
	
	import com.app.model.GameConfigProxy;
	import com.app.model.xml.AnalysisXMLProxy;
	import com.app.model.HttpProxy; 
	
	import com.app.view.AppMediator;
	import com.app.view.manager.LayerManagerMediator;
	import com.app.view.manager.DialogManagerMediator;
	
	import com.peachcastle.debug.Log;

	/**
	 * ...
	 * @author kevin
	 */
	public class InitializeCommand extends SimpleCommand implements ICommand
	{
		private var _queueLoader:QueueLoader = null;
		
		override public function execute(notification:INotification):void
		{
			var params:* = notification.getBody();
			init(params[0], params[1]);
		}
		
		private function init(view:Object, data:Object):void
		{
			gameConfigP.setStageSize(760, 700);
			gameConfigP.initLog();
			gameConfigP.parseUserFromWeb(data);
			gameConfigP.parseConfigFromWeb(data);
			httpP.init(gameConfigP.webs["api_path"], gameConfigP.webs["feedback_path"]);
			
			sendNotification(LayerManagerMediator.SET_ROOT_LAYER, [view, gameConfigP.stageWidth, gameConfigP.stageHeight]);
			sendNotification(LayerManagerMediator.SET_DIALOG_LAYER, [gameConfigP.getDialogUrl, gameConfigP.stageWidth, gameConfigP.stageHeight]);
			
			loadXml();
		}
		
		private function loadXml():void
		{
			Log.debug("loadXml");
			
			gameConfigP.xmlManager.addEventListener(Event.COMPLETE, onXMLComplete);
			gameConfigP.xmlManager.addFile(gameConfigP.getXmlUrl("system"), "system");
			gameConfigP.xmlManager.addFile(gameConfigP.getXmlUrl("server"), "server");
			gameConfigP.xmlManager.start();
		}
		
		private function onXMLComplete(e:Event):void
		{
			gameConfigP.xmlManager.removeEventListener(Event.COMPLETE, onXMLComplete);
			
			analysisXmlP.analysisSystem(gameConfigP.xmlManager.getLabelByXML("system"));
			analysisXmlP.analysisServers(gameConfigP.xmlManager.getLabelByXML("server"));
			
			loadHallSwf();
		}
		
		private function loadHallSwf():void
		{
			Log.debug("loadHallSwf");
			
			_queueLoader = new QueueLoader();
			_queueLoader.addEventListener(LoaderEvent.LOADERALL_COMPLETE, onLoadHallComplete);
			_queueLoader.addEventListener(LoaderEvent.LOADER_PROGRESS, onLoadHallProgress);
			_queueLoader.addItem(gameConfigP.getSwfUrl('sound'), "sound");
			_queueLoader.addItem(gameConfigP.getSwfUrl('hall'), "hall");
			_queueLoader.start();
		}
		
		private function onLoadHallProgress(e:LoaderEvent):void
		{
			sendNotification(AppMediator.HALL_LOAD_PROGRESS, [e.progress]);
		}
		
		private function onLoadHallComplete(event:LoaderEvent):void
		{
			_queueLoader.removeEventListener(LoaderEvent.LOADERALL_COMPLETE, onLoadHallComplete);
			_queueLoader.removeEventListener(LoaderEvent.LOADER_PROGRESS, onLoadHallProgress);
			
			sendNotification(AppMediator.HALL_LOAD_OK);
			
			sendNotification(AppFacade.INIT_HALL, [gameConfigP.hallIp, gameConfigP.hallPort]);
		}
		
		private function get gameConfigP():GameConfigProxy
		{
			return facade.retrieveProxy(GameConfigProxy.NAME) as GameConfigProxy;
		}	
		
		private function get analysisXmlP():AnalysisXMLProxy
		{
			return facade.retrieveProxy(AnalysisXMLProxy.NAME) as AnalysisXMLProxy;
		}
		
		private function get httpP():HttpProxy
		{
			return facade.retrieveProxy(HttpProxy.NAME) as HttpProxy;
		}
	}
}